package chatspace.managedBeans;

import chatspace.dao.Dao;
import chatspace.entities.User;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author lhoussin
 */
public class SearchMB implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * Creates a new instance of SearchMB
     */
    String searchFor;
    List<User> foundUsers;
    List<User> selectedUsers = new ArrayList<User>();
    private Logger logger = Logger.getLogger(ChatMB.class.getName());

    public SearchMB() {
    }

    public String search() {
        logger.info("searching for : " + searchFor);
        foundUsers = new Dao().searchForUsers(searchFor);
        foundUsers.removeAll(selectedUsers);
        return "";
    }

    public void select(User user) {
        int index = foundUsers.indexOf(user);
        logger.info("index of the user :"+index);
        logger.info("avant : "+foundUsers.size());
        User lastUser = foundUsers.get(foundUsers.size() - 1);
        foundUsers.set(index, lastUser);
        logger.info("après : "+foundUsers.size());
        selectedUsers.add(user);
        foundUsers.remove(foundUsers.size() - 1);
        logger.info("après après :"+foundUsers.size());
        
    }
    

    public String getSearchFor() {
        return searchFor;
    }

    public void setSearchFor(String searchFor) {
        this.searchFor = searchFor;
    }

    public List<User> getFoundUsers() {
        return foundUsers;
    }

    public void setFoundUsers(List<User> foundUsers) {
        this.foundUsers = foundUsers;
    }

    public List<User> getSelectedUsers() {
        return selectedUsers;
    }

    public void setSelectedUsers(List<User> selectedUsers) {
        this.selectedUsers = selectedUsers;
    }

    public static void main(String args[]) {
        SearchMB smb = new SearchMB();
        smb.setSearchFor("qsf");
        smb.search();
        System.out.println(smb.getFoundUsers());
    }
}